﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Office.Interop;

namespace LDAPOfficeTool
{
    public partial class Person2Display : UserControl
    {
        int iId;

        LDAPServiceNS.PersonBean person2Display;
        LDAPServiceNS.OrganisationBean org2Display;
        Microsoft.Office.Interop.Word.Document doc2Handle;
        string sPersonMainOrg;


        public string GetTelephone()
        {
            string s2Return;
            s2Return = "";
            if (person2Display.telefoneNumber != null)
            {
                s2Return = s2Return + person2Display.telefoneNumber[0];
                for (int i = 1; i < person2Display.telefoneNumber.Length; i++ )
                {
                    s2Return = s2Return + ", " + person2Display.telefoneNumber[i];
                }               
            }
            else
            {
                s2Return = "-";
            }
            return s2Return;
        }


        public Person2Display()
        {
            InitializeComponent();
            toolTip1.SetToolTip(lblPerson, "Link to LDAP Behördenverzeichnis");
        }


        public void EnterParameter(Microsoft.Office.Interop.Word.Document pDoc2Handle)
        {
            doc2Handle = pDoc2Handle;
        }


/*
        public void EnterContent(int piId, LDAPServicesNS.PersonBean pPerson, Color pBackColor)
        {
            person2Display = pPerson;
            iId = piId;
            string s2Enter;

            string sLblPerson;
            sLblPerson = "";
            if (person2Display.personalTitle != null)
            {
                if (person2Display.personalTitle.Length > 0)
                {
                    sLblPerson = person2Display.personalTitle + " " + person2Display.givenName;
                }
            }
            sLblPerson = sLblPerson + person2Display.givenName;
            lblPerson.Text = sLblPerson;
            s2Enter = "sn: " + person2Display.sn + Environment.NewLine;
//            s2Enter = s2Enter + "gvinttitle: " +person2Display.gvIntTitle  pichler erweitert
            s2Enter = s2Enter + "gvou 0: " + person2Display.gvOu[0] + Environment.NewLine;
//            s2Enter = s2Enter + "cn: " + person2Display.cn + Environment.NewLine;
            s2Enter = s2Enter + "Postl. Addr: " + person2Display.postalAddress + Environment.NewLine;
            s2Enter = s2Enter + "tel:  " + StringUtils.StringTable2String(person2Display.telefoneNumber) + Environment.NewLine;
            s2Enter = s2Enter + "mail: " + person2Display.mail + Environment.NewLine;
            s2Enter = s2Enter + "gvgid: " + person2Display.gvGid;
            txtPerson.Text = s2Enter;
            txtPerson.BackColor = pBackColor;
        }
 */


        string GetPersonNameWithTitle()
        {
            string sRetVal;
            sRetVal = "";
            if (person2Display.personalTitle != null)
            {
                if (person2Display.personalTitle.Length > 0)
                {
                    sRetVal = person2Display.personalTitle + " ";
                }
            }
            sRetVal = sRetVal + person2Display.givenName + " " + person2Display.sn;
            return sRetVal;
            
        }



        public void EnterContent(int piId, LDAPServiceNS.PersonBean pPerson, Color pBackColor, LDAPServiceNS.OrganisationBean pOrganisation, string psPersonMainOrg)
        {
            person2Display = pPerson;
            org2Display = pOrganisation;
            iId = piId;
            string s2Enter;
            lblPerson.Text = GetPersonNameWithTitle();
            sPersonMainOrg = psPersonMainOrg;

            lblPMainOrg.Text = sPersonMainOrg;
            // be careful, the whole Org can be null
            if (pOrganisation != null)
            {
                lblOrg.Text = pOrganisation.ou + "," + pOrganisation.cn[0];
            }
            else
            {
                lblOrg.Text = "? ob";
            }

// no good if null
//            lblPhone.Text = StringUtils.StringTable2String(person2Display.telefoneNumber);
            lblPhone.Text = StringUtils.StringTable2StringWLNeutral(person2Display.telefoneNumber, "tel ?");


            lblEmail.Text = person2Display.mail;

            s2Enter = "sn: " + person2Display.sn + Environment.NewLine;
            s2Enter = s2Enter + "gvou 0: " + person2Display.gvOu[0] + Environment.NewLine;
            s2Enter = s2Enter + "Postl. Addr: " + person2Display.postalAddress + Environment.NewLine;
            s2Enter = s2Enter + "gvgid: " + person2Display.gvGid + Environment.NewLine;
            txtPerson.Text = s2Enter;
            txtPerson.BackColor = pBackColor;
        }


        private void nameToolStripMenuItem_Click(object sender, EventArgs e)
        {
//            MessageBox.Show ("This is name toolstrip menu items...");
            Microsoft.Office.Interop.Word.Selection vSelection;
            vSelection = doc2Handle.Application.Selection;
            vSelection.TypeText(GetPersonNameWithTitle() + " ");
        }

        private void telefonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Selection vSelection;
            vSelection = doc2Handle.Application.Selection;
            vSelection.TypeText(StringUtils.Table2CommaCollection(person2Display.telefoneNumber) + " ");
        }

        private void lblPerson_MouseClick(object sender, MouseEventArgs e)
        {
            BrowserForm person2LDAP;
            person2LDAP = new BrowserForm();
            string s2Open;          
            s2Open = "http://ldap.gv.at/lfrz.at/ldapw/index.php?sPage=1&showDetailDn=" + person2Display.dn + "&detailType=person";
            person2LDAP.SetUri(s2Open);
            person2LDAP.Visible = true;
        }


        private void lblPerson_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void lblPerson_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void volleAdresseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sText2Enter;
            string[] stAddressParts;
            if (person2Display.postalAddress != null)
            {
                stAddressParts = person2Display.postalAddress.Split('$');
                sText2Enter = GetPersonNameWithTitle() + Environment.NewLine;
                for (int i = 0; i < stAddressParts.Length; i++)
                {
                    sText2Enter = sText2Enter + stAddressParts[i] + Environment.NewLine;
                }
//                doc2Handle.Content.InsertBefore(sText2Enter);
                Microsoft.Office.Interop.Word.Selection vSelection;
                vSelection = doc2Handle.Application.Selection;
                vSelection.TypeText(sText2Enter);

            }
            else
            {
                MessageBox.Show("No postal address available");
            }
        }

        private void descriptionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (person2Display.description != null)
            {
                Microsoft.Office.Interop.Word.Selection vSelection;
                vSelection = doc2Handle.Application.Selection;
                vSelection.TypeText(StringUtils.Table2CommaCollection(person2Display.description));
            }
            else
            {
                MessageBox.Show("No description");
            }

        }

        private void adressatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string sText2Insert;
            sText2Insert = "MAIN org" + sPersonMainOrg + Environment.NewLine;
            sText2Insert = sText2Insert + org2Display.cn[0] + Environment.NewLine;
            sText2Insert = sText2Insert + person2Display.street + ", " + person2Display.postalCode + " " + person2Display.l + Environment.NewLine;
            sText2Insert = sText2Insert + "z.H. " + GetPersonNameWithTitle() + Environment.NewLine;
            sText2Insert = sText2Insert + "Tel: " + GetTelephone() +Environment.NewLine;
            sText2Insert = sText2Insert + person2Display.mail + Environment.NewLine;
//            person2Display.g
            Microsoft.Office.Interop.Word.Selection vSelection;
            vSelection = doc2Handle.Application.Selection;
            vSelection.TypeText(sText2Insert);

        }

        private void btnMail_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Email: " + person2Display.mail);
            var mailtoLink = "mailto://" + person2Display.mail;
            // callTo  (mit skype probieren), callto://01121212")
            // mailto sollte auch gehen

            System.Diagnostics.Process.Start(mailtoLink);
        }
    }

}
